#! /usr/bin/env python

"""
(c) 2007 Alon Zakai ('Kripken') <kripkensteiner@gmail.com>

This software is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public
License along with this library; see the file COPYING
If not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""

### NEED package "fuse-utils", for fusermount
### NEED user to be a member of the group "fuse" (what is command for dis?)
### NEED to execute "chgrp fuse /dev/fuse" on Edgy (bug 1860)

import os

try:
	from pysqlite2 import dbapi2 as sqlite
except ImportError:
	import sqlite3 as sqlite

DB_NAME = "ODMDB"

class ODMDB:
	def __init__(self, path_to_DB):
		# Create the DB, if needed
		if DB_NAME not in os.listdir(path_to_DB):
			self.create_from_scratch(path_to_DB)

		# Connect
		print "Connecting to DB..."
		self.conn = sqlite.connect(path_to_DB + os.sep + DB_NAME)

	def create_from_scratch(self, path_to_DB):
		print "Creating DB..."
		conn = sqlite.connect(path_to_DB + os.sep + DB_NAME)

		# Create Tables
		conn.execute("""
			create table if not exists ODM_files
				(id             integer primary key not null,
				 name           text    not null,
				 real_loc       text    not null,
				 date_added     integer not null,
				 use_versioning integer not null default 1,
				 last_commit    integer default null
				)
			""")

		conn.execute("""
			create table if not exists ODM_tags
				(id        integer primary key not null,
				 name      text    not null)
			""")

		conn.execute("""
			create table if not exists ODM_filetags
				(file_id integer not null,
				 tag_id  integer not null)
			""")

		conn.execute("""
			create table if not exists ODM_lists
				(id        integer primary key not null,
				 name      text    not null,
				 parent_id integer default null)
			""")

		# Create standard data for tables

		conn.execute("insert into ODM_lists values (1, 'All Documents')")
		conn.execute("insert into ODM_lists values (2, 'Current List')")
		conn.execute("insert into ODM_lists values (3, 'Saved Lists')")
